/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package TimeRecording.tables;

import TimeRecording.business.DaisyChainUtilities;
import TimeRecording.business.DateRange;
import TimeRecording.enums.LeaveAllowanceStrategy;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.Serializable;
import java.util.GregorianCalendar;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Transient;

/**
 *
 * @author david
 */
@Entity
@Table(name = "control")
@NamedQueries({
    @NamedQuery(name = "Control.findAll", query = "SELECT c FROM Control c"),
    @NamedQuery(name = "Control.findByFixedId", query = "SELECT c FROM Control c WHERE c.fixedId = :fixedId")})

public class Control implements Serializable {

    public static Short FIXED_ID = 1;
    
    private static final long serialVersionUID = 1L;

    // <editor-fold defaultstate="collapsed" desc="Property Change Support">
    @Transient
    private PropertyChangeSupport changeSupport = new PropertyChangeSupport(this);

    public void addPropertyChangeListener(PropertyChangeListener listener) {
        changeSupport.addPropertyChangeListener(listener);
    }

    public void removePropertyChangeListener(PropertyChangeListener listener) {
        changeSupport.removePropertyChangeListener(listener);
    } // </editor-fold>

    @Id
    @Basic(optional = false)
    @Column(name = "fixed_id")
    private Short fixedId;

    @Basic(optional = false)
    @Column(name = "window_weeks_forward")
    private short windowWeeksForward;

    @Basic(optional = false)
    @Column(name = "window_weeks_backward")
    private short windowWeeksBackward;

    @Basic(optional = false)
    @Column(name = "leave_allowance_strategy")
    private LeaveAllowanceStrategy leaveAllowanceStrategy;

    public Control() {
    }

    public Control(Short fixedId) {
        this.fixedId = fixedId;
    }

    public Control(Short fixedId, short windowWeeksForward, short windowWeeksBackward) {
        this.fixedId = fixedId;
        this.windowWeeksForward = windowWeeksForward;
        this.windowWeeksBackward = windowWeeksBackward;
    }

    public Short getFixedId() {
        return fixedId;
    }

    public void setFixedId(Short fixedId) {
        Short oldFixedId = this.fixedId;
        this.fixedId = fixedId;
        changeSupport.firePropertyChange("fixedId", oldFixedId, fixedId);
    }

    public short getWindowWeeksForward() {
        return windowWeeksForward;
    }

    public void setWindowWeeksForward(short windowWeeksForward) {
        short oldWindowWeeksForward = this.windowWeeksForward;
        this.windowWeeksForward = windowWeeksForward;
        changeSupport.firePropertyChange("windowWeeksForward", oldWindowWeeksForward, windowWeeksForward);
    }

    public short getWindowWeeksBackward() {
        return windowWeeksBackward;
    }

    public void setWindowWeeksBackward(short windowWeeksBackward) {
        short oldWindowWeeksBackward = this.windowWeeksBackward;
        this.windowWeeksBackward = windowWeeksBackward;
        changeSupport.firePropertyChange("windowWeeksBackward", oldWindowWeeksBackward, windowWeeksBackward);
    }

    public LeaveAllowanceStrategy getLeaveAllowanceStrategy() {
        return leaveAllowanceStrategy;
    }

    public void setLeaveAllowanceStrategy(LeaveAllowanceStrategy leaveAllowanceStrategy) {
        LeaveAllowanceStrategy oldLeaveAllowanceStrategy = this.leaveAllowanceStrategy;
        this.leaveAllowanceStrategy = leaveAllowanceStrategy;
        changeSupport.firePropertyChange("leaveAllowanceStrategy", oldLeaveAllowanceStrategy, leaveAllowanceStrategy);
    }

    /**
     * Returns the date range of the "active window"; that is, a number of weeks
     * back from the current date to a number of weeks into the future.  The
     * date range is Monday to Monday such that, with a one week window either
     * side, the start date would be Monday of last week and the end date would
     * be the Monday of next week.
     */
    public static DateRange getActiveWindow(EntityManager entityManager) {

        Control control = entityManager.find(Control.class, Control.FIXED_ID);

        // Create a window stretching 'n1' weeks back to 'n2' weeks forward

        GregorianCalendar startOfWeek =
                DaisyChainUtilities.firstDayOfWeek((GregorianCalendar)null);

        // Move the start of our window back 'n1' weeks

        GregorianCalendar startOfWindow =
                (GregorianCalendar) startOfWeek.clone();
        startOfWindow.add(GregorianCalendar.WEEK_OF_YEAR,
                -control.getWindowWeeksBackward());

        // Move the end of our window back 'n2' weeks

        GregorianCalendar endOfWindow =
                (GregorianCalendar) startOfWeek.clone();
        endOfWindow.add(GregorianCalendar.WEEK_OF_YEAR,
                control.getWindowWeeksForward());

        return new DateRange(startOfWindow.getTime(), endOfWindow.getTime());
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (fixedId != null ? fixedId.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Control)) {
            return false;
        }
        Control other = (Control) object;
        if ((this.fixedId == null && other.fixedId != null) || (this.fixedId != null && !this.fixedId.equals(other.fixedId))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "TimeRecording.tables.Control[fixedId=" + fixedId + "]";
    }

}
